import json
from datetime import datetime,date, timedelta

from django.core.paginator import Paginator
from django.db import transaction
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from rest_framework import serializers
from rest_framework.generics import ListCreateAPIView, ListAPIView
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import GoodsVisitCount, SPUSpecification, SPU, SKUSpecification

from apps.meiduo_admin.serializers.specs_serializers import SPUSpecificationSerializer, SPUSerializer
from apps.meiduo_admin.utils.pagenum import PageNum
from apps.orders.models import OrderInfo
from apps.statistical.serializers import GoodSerializer
from apps.users.models import User
from django.utils import timezone
from rest_framework import viewsets
from apps.goods.models import SKU , GoodsCategory
from celery_tasks.detail.tasks import get_detail_html



# 商品规格管理
class SpecsViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    # 指定序列化器
    serializer_class = SPUSpecificationSerializer
    queryset = SPUSpecification.objects.all().order_by("id")
    # 指定分页器
    pagination_class = PageNum
# 新增商品规格
class SPUSimpleView(ModelViewSet):
    serializer_class = SPUSerializer
    queryset = SPU.objects.all()

# # ===========================
# # Django版开发增删改查
# # 复数:查询多个  创建
# # 单数:查询一个  删除  修改
# class SpecsView(View):
#     def get(self,request):
#         queryset = SPUSpecification.objects.all()
#         # 接受分页大小
#         page_size = int(request.GET.get("pagesize"))
#         page_index = int(request.GET.get("page"))
#         paginator = Paginator(queryset,page_size)
#         page = paginator.page(page_index)
#
#         spec_list = []
#         for spec in page:
#             spec_list.append({
#                     "id":spec.id,
#                     "name":spec.name,
#                     "spu":spec.spu.name,
#                     "spu_id":spec.spu_id
#                 })
#         return JsonResponse({
#             "counts":paginator.count,
#             "list":spec_list,
#             "page":page_index,
#             "pages":paginator.num_pages,
#             "pagesize":page_size
#         })
#
#     def post(self,request):
#         # 接受
#         json_dict = json.loads(request.body.decode())
#         name = json_dict.get("name")
#         spu_id = json_dict.get("spu_id")
#         # 验证
#         if not all([name,spu_id]):
#             return JsonResponse({"msg":"数据不完整"},status=400)
#
#         if  SPU.objects.filter(pk=spu_id).count()<=0:
#             return JsonResponse({"msg": "spu编号无效"}, status=400)
#         # 处理
#         instance= SPUSpecification.objects.create(name=name,spu_id=spu_id)
#         # 响应
#         return JsonResponse({
#             "id":instance.id
#         })
#
# class SpecView(View):
#     def get(self,request,pk):
#         pass
#     def put(self,request,pk):
#         pass
#     def delete(self,request,pk):
#         pass